package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.socialize.utils.Log;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.EntiretyBathroom2Bean;
import com.wnhz.work.yz.bean.Finish1;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * ---整体卫浴详情
 */
public class EntiretyBathroom2Activity extends BaseActivity implements View.OnClickListener {
    private ViewPager vp_bathroom;
    private TextView tv_title;
    private TextView tv_name;

    private String cateId;
    private ArrayList<EntiretyBathroom2Bean> beanList = new ArrayList<>();
    private String titles;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Finish1 event) {
        finish();
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);//反注册EventBus
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_entirety_bathroom2);
        EventBus.getDefault().register(this);
        initData();

        initView();
    }

    private void initData() {
        cateId = getIntent().getStringExtra("cateId");
        titles = getIntent().getStringExtra("titles");

    }

    private void initView() {
        vp_bathroom = (ViewPager) findViewById(R.id.vp_bathroom);

        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_title.setText(titles);
    }

    private void initListener() {

        findViewById(R.id.iv_back).setOnClickListener(this);
        findViewById(R.id.iv_right_jump).setOnClickListener(this);
        vp_bathroom.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
        upload();
    }

    private void upload() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("cateId", cateId);
        XUtil.Post(Url.PACKAGE_GETPACKAGELIST, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                super.onSuccess(result);

                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    JSONArray jsonArray = jsonObject.optJSONArray("info");
                    Log.e("====旅逸===", result);
                    if (jsonArray != null) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            EntiretyBathroom2Bean bean = new EntiretyBathroom2Bean();
                            Gson gson = new Gson();
                            bean = gson.fromJson(jsonArray.optString(i), EntiretyBathroom2Bean.class);
                            beanList.add(bean);
                        }
                        tv_name.setText(beanList.get(0).getGoods_name());
                    }

                    initViewPager();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                initListener();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;

            case R.id.iv_right_jump:
                intent = new Intent(this, BathroomGridActivity.class);
                intent.putExtra("title", titles);
                intent.putExtra("cateId", cateId);
                startActivity(intent);
                break;
            default:
                break;
        }
    }


    private void initViewPager() {
        String[] arrayStr = new String[beanList.size()];
        for (int i = 0; i < beanList.size(); i++) {
            arrayStr[i] = beanList.get(i).getIndex_pic();
        }


        vp_bathroom.setAdapter(new ViewPagetAdapter(arrayStr));
        setViewPager();
    }

    private void setViewPager() {
        vp_bathroom.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(final int arg0) {
                currentPosition = arg0;
                tv_name.setText(beanList.get(arg0).getGoods_name());
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });

        currentPosition = 0;
        vp_bathroom.setCurrentItem(currentPosition, false);
    }

    private int currentPosition;

    private class ViewPagetAdapter extends PagerAdapter {
        private String[] mImageViews;

        public ViewPagetAdapter(String[] mImageViews) {
            this.mImageViews = mImageViews;
        }

        @Override
        public int getCount() {
            return mImageViews.length;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {

            container.removeView((View) object);
        }

        /**
         * 载入图片进去，用当前的position 除以 图片数组长度取余数是关键
         */
        @Override
        public Object instantiateItem(View container, final int position) {
            ImageView imageView = new ImageView(EntiretyBathroom2Activity.this);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

//            ImageLoader.getInstance().displayImage(mImageViews[position], imageView);
            Glide.with(EntiretyBathroom2Activity.this).load(mImageViews[position]).into(imageView);
            ((ViewPager) container).addView(imageView);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(EntiretyBathroom2Activity.this, BathroomDetailActivity.class);
                    intent.putExtra("packageId", beanList.get(position).getGoodsid());
                    startActivity(intent);
                }
            });
            return imageView;

        }


    }
}
